﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//Ljj 2019.6.11
//控制移动的组件
public class Move : MonoBehaviour
{
    public Selector roadSelector = Selector.Random;
    public float speed = 1.5f;
    public Vector2Int logicPos;
    public Vector3 offset;
    Vector3 worldPos;
    Vector3 targetWorldPos;
    Vector2Int targetLogicPos;

    float objectWidth = 0.15f;
    float objectHeight = 0.15f;
    Vector3 originPos = new Vector3(0, 0, 0);

    Animator animator;
    BattleObject battle;
    bool isWalking;

    // Start is called before the first frame update
    void Start()
    {

        targetLogicPos = logicPos;
        worldPos = transform.position;
        targetWorldPos = worldPos;
        isWalking = false;
        animator = gameObject.GetComponent<Animator>();
        battle = gameObject.GetComponent<BattleObject>();
        UpdatePosition();
        
    }

    // Update is called once per frame
    void Update()
    {
        if (battle != null && !battle.CanWalk())
        {
            UpdateWalkParam(false);
            return;
        }
        //到达目标坐标
        if (Function.DistanceIgnoreZ(targetWorldPos, worldPos) <= 0.01) 
        {
            logicPos = targetLogicPos;
            RoadSelectResult result = this.GetComponent<RoadSelector>().GetNextPos(logicPos, roadSelector);
            targetLogicPos = result.Pos();
            //Debug.Log(nowLogicPos + " " + targetLogicPos + " "+ result.Tag().ToString());  

            RoadSelectTag tag = result.Tag();
            targetWorldPos = Function.LogicPosToWorldPos(targetLogicPos);
            targetWorldPos += offset;

            if (tag == RoadSelectTag.Tower)
            {
                targetWorldPos.x = System.Math.Min(targetWorldPos.x, originPos.x + ConstOfMap.xGap * logicPos.x + 0.5f * ConstOfMap.xGap - objectWidth / 2.0f);
                targetWorldPos.x = System.Math.Max(targetWorldPos.x, originPos.x + ConstOfMap.xGap * logicPos.x - 0.5f * ConstOfMap.xGap + objectWidth / 2.0f);
                targetWorldPos.y = System.Math.Min(targetWorldPos.y, originPos.y + ConstOfMap.yGap * logicPos.y + 0.5f * ConstOfMap.yGap - objectHeight / 2.0f);
                targetWorldPos.y = System.Math.Max(targetWorldPos.y, originPos.y + ConstOfMap.yGap * logicPos.y - 0.5f * ConstOfMap.yGap + objectHeight / 2.0f);
                targetLogicPos = logicPos;
            }
        }
        Vector3 targetForward = targetWorldPos - worldPos;
        targetForward.z = 0;
        //if (targetForward.x > 0.1f && targetForward.y > 0.1f)
        //    Debug.LogError("斜着走了:"+logicPos+" "+targetLogicPos+" "+worldPos+" " + targetWorldPos + " " + targetForward); 
        float len = targetForward.magnitude;
        float delta = Time.deltaTime * ConstOfMap.xGap;  
        if (len >= delta * speed)
        {
            targetForward = targetForward.normalized * delta * speed; 
        }
        UpdateWalkParam(Function.DistanceIgnoreZ(targetForward, Vector3.zero) > 0.001f);
        if (Mathf.Abs(targetForward.x) > 0.001f)
            battle.UpdateMirrorParam(targetForward.x < 0.001f);
        worldPos += targetForward;
        UpdatePosition();
    }

    void UpdateWalkParam(bool isWalking)
    {
        if (this.isWalking != isWalking)
        {
            this.isWalking = isWalking;
            animator.SetBool("Walk", isWalking);
        }
    }

    //修改物体位置
    void UpdatePosition()
    {
        worldPos.z = worldPos.y * 0.1f;
        this.GetComponent<Transform>().localPosition = worldPos;
    }
}
